﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rapattern.Utilities.字符串操作
{
    /// <summary>
    /// 阿拉伯数字与中文数字相互转换
    /// </summary>
    public class Number_ChinaHelper
    {
        /// <summary>
        /// 小写中文数字转阿拉伯数字
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int ConvertChinaToNumber(string data)
        {
            int number = 0;
            string[] numArr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string chinStr = "零一二三四五六七八九";
            string bigStr = "十百千万十百千亿";
            string pad = "1";

            if (data.StartsWith("十"))
            {
                number = 1;
            }
            int count = data.Length;
            for (int i = 0; i < count; i++)
            {
                string li = data.Substring(i, 1);
                int chinIndex = chinStr.IndexOf(li);
                int bigIndex = bigStr.IndexOf(li);
                if (chinIndex > -1)
                {
                    number += Convert.ToInt32(numArr[chinIndex]);
                }
                else if (bigIndex > -1)
                {
                    number *= Convert.ToInt32(pad.PadRight(bigIndex + 2, '0'));
                }
            }

            return number;
        }
        /// <summary>
        /// 阿拉伯数字转中文小写数字
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string ConvertNumberToChina(int num)
        {
            string[] intArr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", };
            string[] strArr = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", };
            string[] Chinese = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
            char[] tmpArr = num.ToString().ToArray();
            string tmpVal = "";
            for (int i = 0; i < tmpArr.Length; i++)
            {
                tmpVal += strArr[tmpArr[i] - 48];//ASCII编码 0为48
                tmpVal += Chinese[tmpArr.Length - 1 - i];//根据对应的位数插入对应的单位
            }
            if (tmpVal.StartsWith("一十"))
            {
                tmpVal = tmpVal.Replace("一十", "十");
            }
            return tmpVal.TrimEnd("零".ToArray());
        }
    }
}
